<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2019/8/30
 * Time: 14:26
 */
namespace app\apiteacher\controller;
use think\Db;
class One extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $user_id2 = input('user_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($user_id2,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    /**
     * 开启或关闭一对一
     * $type 1开启  2关闭
     * @return \think\response\Json
     */
    public function openClose()
    {
        extract(input());
        $res = Db::table('up_u_user')->where('user_id',$user_id)->update([
            'user_out' => $type,
            'user_online' => 1
        ]);
        return json(['status' => 200, 'msg' => '操作成功','type'=>$type]);
    }
    /** 一对一的申请列表
     * 返参：1待接收    2 待上课   3 上课中    4 已上课  5已关闭  6拒绝
     * @return \think\response\Json
     */
    public function index(){
        extract(input());
        $where = array();
        $where['one_teacher_id'] = $user_id;
        $field = 'o.*,user_username';
        $res = Db::table('up_one_to_one')->alias('o')
            ->join('up_u_user u','o.one_student_id = u.user_id','left')
            ->where($where)
            ->order('one_status asc')
            ->field($field)
            ->page($page,10)
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }
    /** 改变当前状态
     * type：1待接收（接受传2，拒绝传6）   2 待上课（连接学生上课传3）    3 上课中（继续上课不用传 结束传4）
     * @return \think\response\Json
     */
    public function indexChangeStatus(){
        extract(input());
        $update = array();
        $data = Db::table('up_one_to_one')
            ->where('one_id',$one_id)
            ->find();
        if($type != 2 && $type != 6 && $type != 3 && $type != 4){
            return  json(array('status'=>100,'msg'=>'非法请求'));
        }
        if($type == 2){
            $have = Db::table('up_one_to_one')
                ->where('one_id','<>',$one_id)
                ->where('one_teacher_id',$user_id)
                ->where('one_status','in','2,3') //有可能有好几个都来申请，只看有没有没有结束的就行
                ->find();
            if($have){
                return  json(array('status'=>100,'msg'=>'您有待支付或者未完成的课'));
            }
            if($data['one_status'] == 5){
                return  json(array('status'=>111,'msg'=>'该课程已关闭'));
            }
            if($data['one_status'] != 1){
                return  json(array('status'=>100,'msg'=>'只有待接受才可以同意'));
            }
            $update['one_agree_pay_time'] = date('Y-m-d H:i:s'); //同意时间
        }
        if($type == 6){
            if($data['one_status'] != 1){
                return  json(array('status'=>100,'msg'=>'只有待接受才可以拒绝'));
            }
        }
        if($type == 3){
            if($data['one_status'] != 2){
                return  json(array('status'=>100,'msg'=>'只有待上课的才可以连接'));
            }
            $update['one_class_time'] = date('Y-m-d H:i:s');
        }
        if($type == 4){
            if($data['one_status'] != 3){
                return  json(array('status'=>100,'msg'=>'只有上课中的才可以结束'));
            }
            $update['one_end_time'] = date('Y-m-d H:i:s');
        }
        $update['one_status'] = $type;
        Db::startTrans();
        try{
            if($type == 6){
                //返还学生钱
                $b_money = Db::table('up_u_user')->where('user_id',$data['one_teacher_id'])->value('user_toll');
                Db::table('up_u_user')->where('user_id',$data['one_student_id'])->setInc('user_balance',$b_money);
                //学生入order增并删除记录
                allInOrder(3,$b_money,$data['one_student_id'],1);
                Db::table('up_purchase')->where('purchase_type',3)->where('f_id',$one_id)->delete();
            }
            if($type == 2){ // 接受请求
                // 入消息表
                allInNotice('一对一辅导', '有老师接受了你的一对一辅导申请，快来看看~', 1, 2, 1, -1, $data['one_student_id']);
                // 推送
                $list = [];//需要推送的用户数组
                $val = (string)$data['one_student_id'];
                array_push($list, $val);
                $this->JpushS(1, $list, '一对一辅导', ['send_type' => 1]);
            }
            if($type == 4){ // 结束加经验分
                Db::table('up_u_user')->where('user_id', $data['one_teacher_id'])->setInc('user_exper');
            }
            Db::table('up_one_to_one')->where('one_id',$one_id)->update($update);
            Db::commit();
            return  json(array('status'=>200,'msg'=>'操作成功'));
        }catch (\Exception $e) {
            Db::rollback();
            return  json(array('status'=>100,'msg'=>'操作失败'));
        }
    }

    /**
     * 每一次返回的时候更新下那个倒计时
     */
    public function updateClock()
    {
        extract(input());
        $res = Db::table('up_one_to_one')->where('one_id',$one_id)->setField('one_class_time',$clock);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 用于更新上课剩余时间
     */
    public function updateClassTime(){
        $one_id = input('one_id');
        $class_time = input('class_time');
        // 防止负数的情况出现
        $second = Db::table('up_one_to_one')->where('one_id', $one_id)->value('one_second');
        if($second <= $class_time){
            Db::table('up_one_to_one')->where('one_id', $one_id)->setField('one_second', 0);
        }else{
            Db::table('up_one_to_one')->where('one_id', $one_id)->setDec('one_second', $class_time);
        }
        return json(['status' => 200, 'msg' => 'ok']);
    }
}